import { Command } from '../constant';
import Taro from '@tarojs/taro';
import { Dispatch } from 'typings';
import { IAllReducerProps } from '../types';
import { getReducerData } from '@/redux/store';
import { extraPathsValue } from '@/redux/util';
import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    async page(params) {
      let res = await api.pagesIndexController.selectDistance(params)
      if (res.code == '200' && res.data && res.data.length > 0) {
        action.commonChange('main.list', res.data);
      } else {
        action.commonChange('main.list', []);
        Taro.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000,
        });
      }
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageASelectAddressMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
